/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: UsersServiceImpl.java
 * Date: 01/03/2011
 */

package com.ingenius.sau.server.service;

import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.ingenius.sau.client.service.UsersService;
import com.ingenius.sau.server.persistence.DAOFactory;
import com.ingenius.sau.server.persistence.GroupDAO;
import com.ingenius.sau.server.persistence.RoleDAO;
import com.ingenius.sau.server.persistence.UserDAO;
import com.ingenius.sau.server.persistence.jdo.JdoDAOFactory;
import com.ingenius.sau.shared.model.users.Group;
import com.ingenius.sau.shared.model.users.Role;
import com.ingenius.sau.shared.model.users.User;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>UsersServiceImpl</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
@SuppressWarnings("serial")
public class UsersServiceImpl extends AbstractService implements UsersService {

	private DAOFactory daoFactory;
	private UserDAO userDAO;
	private RoleDAO roleDAO;
	private GroupDAO groupDAO;

	public UsersServiceImpl() {
		daoFactory = JdoDAOFactory.getInstance();
		userDAO = daoFactory.createUserDAO();
		roleDAO = daoFactory.createRoleDAO();
		groupDAO = daoFactory.createGroupDAO();
	}

	@Override
	public Boolean saveOrUpdateUser(User user) {
		if (user.getId() == null) {
			return userDAO.save(user);
		}
		return userDAO.update(user);
	}

	@Override
	public Boolean deleteUser(String id) {
		return userDAO.delete(id);
	}

	@Override
	public User getUser(String id) {
		return userDAO.get(id);
	}

	@Override
	public PagingLoadResult<User> findUsers(List<Filter> filters,
			BasePagingLoadConfig config) {

		return super.find(userDAO, filters, config);
	}

	@Override
	public Boolean saveOrUpdateRole(Role role) {
		if (role.getId() == null) {
			return roleDAO.save(role);
		}
		return roleDAO.update(role);
	}

	@Override
	public Boolean deleteRole(String id) {
		return roleDAO.delete(id);
	}

	@Override
	public Role getRole(String id) {
		return roleDAO.get(id);
	}

	@Override
	public PagingLoadResult<Role> findRoles(List<Filter> filters,
			BasePagingLoadConfig config) {

		return super.find(roleDAO, filters, config);
	}

	@Override
	public List<Role> loadAllRoles() {
		int recordsCount = roleDAO.count(null).intValue();
		List<Role> result = roleDAO.getRange(null, null, 0, recordsCount);
		for (Role role : result) {
			role = role.toDTO();
		}
		return result;
	}

	@Override
	public Boolean saveOrUpdateGroup(Group group) {
		if (group.getId() == null) {
			return groupDAO.save(group);
		}
		return groupDAO.update(group);
	}

	@Override
	public Boolean deleteGroup(String id) {
		return groupDAO.delete(id);
	}

	@Override
	public Group getGroup(String id) {
		return groupDAO.get(id);
	}

	@Override
	public PagingLoadResult<Group> findGroups(List<Filter> filters,
			BasePagingLoadConfig config) {

		return super.find(groupDAO, filters, config);
	}

	@Override
	public Boolean hasPermission(String module, String action) {
		try {
			User currentUser = userDAO.userFor(getUsername());
			if (currentUser != null) {
				String param = module + "." + action + "=yes";
				for (String value : currentUser.getRole().getPrivileges()) {
					if (value.equalsIgnoreCase(param)) {
						return true;
					}
				}
			}

		} catch (Exception e) {
			System.out.println("Error --> " + e.getMessage());
		}
		return false;
	}

}
